package cn.tedu.tea.service;

import cn.tedu.tea.domain.TStock;
import cn.tedu.tea.domain.param.TStockAddNewDTO;
import cn.tedu.tea.domain.param.TStockUpdateDTO;
import cn.tedu.tea.domain.vo.TStockListVO;
import cn.tedu.tea.domain.vo.TStockStandardVO;
import cn.tedu.tea.ex.ServiceException;
import cn.tedu.tea.mapper.TStockMapper;
import cn.tedu.tea.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class TStockServiceImpl implements ITStockService {

    @Autowired
    private TStockMapper tStockMapper;

    @Override
    public void addNew(TStockAddNewDTO tStockAddNewDTO) {
        log.debug("开始处理【添加库存】的业务，参数： {}", tStockAddNewDTO);
        TStock tStock = new TStock();
        BeanUtils.copyProperties(tStockAddNewDTO, tStock);
        int rows = tStockMapper.insert(tStock);
        if (rows != 1){
            String message = "添加库存失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }
    }

    @Override
    public void deleteById(Long id) {
        log.debug("开始处理【根据id删除库存】的业务，参数： {}", id);
        TStockStandardVO tStockStandard = tStockMapper.getStandardById(id);
        if (tStockStandard == null){
            String message = "删除库存失败，要删除的库存不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        int rows = tStockMapper.deleteById(id);
        if (rows != 1){
            String message = "删除库存失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
    }

    @Override
    public void deleteByIds(Long[] ids) {
        log.debug("开始处理【根据id批量删除库存】的业务，参数： {}", ids);
        int rows = tStockMapper.deleteByIds(ids);
        if (rows < 1){
            String message = "删除库存失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
    }

    @Override
    public void updateInfoById(TStockUpdateDTO tStockUpdateDTO) {
        log.debug("开始处理【修改库存信息】的业务，参数：{}", tStockUpdateDTO);
        TStockStandardVO tStockStandard = tStockMapper.getStandardById(tStockUpdateDTO.getId());
        if (tStockStandard == null){
            String message = "修改库存失败，要访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        TStock tStock = new TStock();
        BeanUtils.copyProperties(tStockUpdateDTO, tStock);
        int rows = tStockMapper.update(tStock);
        if (rows != 1){
            String message = "修改库存失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }

    @Override
    public TStockStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据id查询库存详情】的业务，参数： {}", id);
        TStockStandardVO tStockStandard = tStockMapper.getStandardById(id);
        if (tStockStandard == null){
            String message = "查询库存详情失败，要查询的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        return tStockStandard;
    }

    @Override
    public List<TStockListVO> list() {
        log.debug("开始处理【查询库存列表】的业务，无参数");
        List<TStockListVO> list = tStockMapper.list();
        return list;
    }
    
}
